# 守护线程：主线程死亡，该线程也随之死亡

from threading import Thread
from time import sleep


class MyThread(Thread):
    def __init__(self, name):
        Thread.__init__(self)
        self.name = name

    def run(self):
        print(f"线程{self.name}启动")
        for i in range(3):
            print(f"线程{self.name} {i}")
            sleep(1)
        print(f"线程{self.name}结束")


if __name__ == "__main__":
    print("主线程启动")
    t1 = MyThread("foo")
    t2 = MyThread("bar")

    # 设置为守护线程
    t1.daemon = True
    t2.daemon = True

    t1.start()
    t2.start()

    t1.join()
    t2.join()

    print("主线程结束")
